package com.allwees.bs.module.fms.rest;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.allwees.bs.core.modelbase.branch.BranchUser;
import com.allwees.bs.core.modelbase.branch.EBranchType;
import com.allwees.bs.core.modelbase.exception.AuthException;
import com.allwees.bs.core.modelbase.exception.AuthExceptionEnum;
import com.allwees.bs.core.modelbase.vo.R;
import com.allwees.bs.module.core.dynamic.annotation.DynamicInject;
import com.allwees.bs.module.core.rest.BoCrudApi;
import com.allwees.bs.module.fms.entity.MerchantAccount;
import com.allwees.bs.module.fms.req.ApplyWithdrawReq;
import com.allwees.bs.module.fms.req.AuditWithdrawReq;
import com.allwees.bs.module.fms.req.DoBatchWithdrawReq;
import com.allwees.bs.module.fms.req.DoWithDrawFailedReq;
import com.allwees.bs.module.fms.req.DoWithdrawReq;
import com.allwees.bs.module.fms.req.ResetWithdrawPasswordReq;
import com.allwees.bs.module.fms.req.SetWithdrawSecurityInfoReq;
import com.allwees.bs.module.fms.req.query.MerchantAccountQueryReq;
import com.allwees.bs.module.fms.service.MerchantAccountService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * 商户账户操作
 *
 * @author yangylsky
 * @since 2019-12-30
 *
 */
@RestController
@RequestMapping("/backstage/api/fms/merchant_account")
public class MerchantAccountApi extends BoCrudApi<MerchantAccount, MerchantAccountService> {
	/**
	 * 查询默认账户
	 *
	 * @return
	 */
	@GetMapping("/default")
	public R defaultAccount() {
		BranchUser user = getCurrentUser();
		MerchantAccount account = boService.findDefaultMerchantAccount(user.getBranchType(), user.getBranchUuid());
        return R.ok(account);
	}

	@Override
	protected Object _doRetrieve(String uuid) {
		if(EBranchType.PLATFORM != getCurrentUser().getBranchType()) {
			throw new AuthException(AuthExceptionEnum.NO_AUTHORIZE);
		}
		return super._doRetrieve(uuid);
	}

    /**
     * 查询账户分页列表
     *
     * @param page
     * @param req
     * @return
     */
	@DynamicInject(key = "branchUuid",type= DynamicInject.Type.Retailer)
    @GetMapping("/page")
    public R page(Page<MerchantAccount> page, MerchantAccountQueryReq req) {
        IPage<MerchantAccount> retPage = boService.findPage(page, req);
        return R.ok(retPage);
    }

	/**
	 * 设置提现安全信息
	 *
	 * @param req
	 * @return
	 */
	@PostMapping("/set_withdraw_security_info")
	public R setWithdrawSecurityInfo(@RequestBody SetWithdrawSecurityInfoReq req) {
		boService.setWithdrawSecurityInfo(req);
		return R.ok("设置绑定手机和提现密码成功");
	}

	/**
	 * 重置密码
	 *
	 * @param req
	 * @return
	 */
	@PostMapping("/reset_withdraw_password")
	public R resetWithdrawPassword(@RequestBody ResetWithdrawPasswordReq req) {
		boService.resetWithdrawPassword(req);
		return R.ok("重置提现密码成功");
	}

	/**
	 * 申请提现
	 *
	 * @param req
	 * @return
	 */
	@PostMapping("/apply_withdraw")
	public R applyWithdraw(@RequestBody ApplyWithdrawReq req) {
		BranchUser branchUser = getCurrentUser();
		boService.applyWithdraw(req,branchUser);
		return R.ok("申请提现成功");
	}

    /**
     * 审核提现
     *
     * @param req
     * @return
     */
	@PostMapping("/audit_withdraw")
	public R auditWithdraw(@RequestBody AuditWithdrawReq req) {
		boService.auditWithdraw(req);
        return R.ok("审核成功");
	}

    /**
     * 执行提现
     *
     * @param req
     * @return
     */
	@PostMapping("/do_withdraw")
	public R doWithdraw(@RequestBody DoWithdrawReq req) {
		boService.doWithdraw(req);
        return R.ok("执行提现成功");
	}

	/**
	 * 批量执行付款
	 *
	 * @param req
	 * @return
	 */
	@PostMapping("/do_batch_withdraw")
	public R doBatchWithdraw(@RequestBody DoBatchWithdrawReq req) {
		boService.doBatchWithdraw(req);
		return R.ok("批量执行提现成功");
	}


	/**
	 * 执行提现失败
	 *
	 * @param req
	 * @return
	 */
	@PostMapping("/do_withdraw_failed")
	public R doWithdrawFailed(@RequestBody DoWithDrawFailedReq req) {
		boService.doWithdrawFailed(req);
		return R.ok("操作成功");
	}
}
